package gem.database;
/*
 * Connection.java
 *
 * Created on 8 November 2006, 19:50
 *
 */

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;

import java.util.HashMap;
import java.util.Vector;
import java.util.logging.Level;

/**
 * Class containing static methods for basic SQL (JDBC) operations
 */
public class Database
{
  // Connection related
    private static String jdbcURL = "jdbc:mysql://192.168.1.200/gem?user=root&password=d8k24m11&characterSetResults=UTF-8&characterEncoding=UTF8&useUnicode=true";
    private static String jdbcDriver = "com.mysql.jdbc.Driver";

    public static String getJDBCURL() { return jdbcURL; }
    public static String getJDBCDriver() { return jdbcDriver; }

    private static boolean isInit = false;
    
    //===========================================================================================//
    private static boolean init()
    {
    	java.util.Properties prop = new java.util.Properties();
    	java.io.FileInputStream fis = null;
    	
    	try
    	{
    		fis = new FileInputStream("config/sql.properties");
    		
    		prop.load( fis );
    		
    		if( prop.getProperty("jdbcURL") != null)
            {
                System.out.println("found url");
                jdbcURL = prop.getProperty("jdbcURL");
            }
            if( prop.getProperty("jdbcDriver") != null)
            {
                jdbcDriver = prop.getProperty("jdbcDriver");
            }
            
            isInit = true;         
            
            return isInit;
    	}
    	catch( IOException e )
    	{
    		e.printStackTrace();
    		
    		return isInit;
    	}
    	finally
    	{
    		if( fis != null )
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    	}
    	
    }
    //===========================================================================================//
    public static java.sql.Connection getConnection()
    {
        try
        {
        	if( !isInit )
        	{
        		init();
        	}
        	
        	long start = System.currentTimeMillis();
        	
        	do
        	{
        		java.sql.Connection conn = PoolDataSource.getInstance().getConnection();
        		
        		if( conn != null )
        		{
        			return conn;
        		}
        		
        		long currTime = System.currentTimeMillis();
        		
        		if( currTime - start >= 15000 )
        		{
        			throw new Exception("Connection error");
        		}
        	}
        	while( true );
        }
        catch( SQLException e )
        {
            e.printStackTrace();
        }
        catch (Exception e )
        {
            e.printStackTrace();
        }

        return null;
    }  
    //===========================================================================================//
    public static java.sql.Connection getConnection( String url )
    {
    	try
    	{
    		// url is passed, and driver is assumed to be mysql
			return PoolDataSource.getInstance(url).getConnection();
		}
    	catch (SQLException e)
    	{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    //===========================================================================================//
}
